package linkedlist;

import java.util.Objects;

public class LinkedList<E> {
    /**
     * 定义私有节点类，用户不需要了解链表的底层实现
     * 用户只需要知道链表是一个线性的数据结构，可以增删改查数据
     * 对外部的用户屏蔽底层的实现
     */
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node dummyHead;
    private int size;

    public LinkedList() {
        dummyHead = new Node(null, null);
        size = 0;
    }

    /**
     * 获取链表中的元素个数
     *
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     * 判断链表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }


    /**
     * @param index
     * @param e
     */
    public void add(int index, E e) {
        if (index < 0 || index > size) throw new IllegalArgumentException("Add failed. Illegal index");
        //1. 找到要插入索引的节点之前的节点；（链表没有索引，这里只是借助索引这个概念）
//        if (index == 0) {
//            addFirst(e);
//        } else {
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) prev = prev.next;
        //2.新建一个节点，
        prev.next = new Node(e, prev.next);
        size++;
//        }
    }

    /**
     * 在链表头部添加新的元素e
     */
    public void addFirst(E e) {
//        Node node = new Node(e);
//        node.next = head;
//        head = node;
//        head = new Node(e, head);
//        size++;
        add(0, e);
    }

    /**
     * @param e
     */
    public void addLast(E e) {
        add(getSize(), e);
    }

    /**
     * 在链表中不是一个常见的操作，练习用
     * 如果想要找index位置前一个位置的元素，就从dummyHead开始
     * 如果想要找index位置的元素，就从head开始找
     *
     * @param index
     * @return
     */
    public E get(int index) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("get failed, illegal index");
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) cur = cur.next;
        return cur.e;
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    public void set(int index, E e) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("set failed, illegal index");
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) cur = cur.next;
        cur.e = e;
    }

    /**
     * 查找链表中是否有元素e
     *
     * @param e
     * @return
     */
    public boolean contains(E e) {
        Node cur = dummyHead.next;
//        for (int i = 0; i < size; i++, cur = cur.next) {
//            if (Objects.equals(e, cur.e)) {
//                return true;
//            }
//        }
        while (cur != null) {
            if (Objects.equals(cur.e, e))
                return true;
            cur = cur.next;
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
//        Node curr = dummyHead.next;
//        while (curr != null) {
//            res.append(curr.e + "->");
//            curr = curr.next;
//        }
        for (Node curr = dummyHead.next; curr != null; curr = curr.next) res.append(curr.e + "->");
        res.append("NULL");
        return res.toString();
    }

    public E remove(int index) {
        if (index < 0 || index >= size) throw new IllegalArgumentException("remove failed, illegal index");
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) prev = prev.next;
        Node ret = prev.next;
        prev.next = prev.next.next;
        ret.next = null;//让节点与链表脱离
        size--;
        return ret.e;
    }

    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }
}
